import java.security.SecureRandom;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.IntStream;

public class CompletableFutureMain {

    public static void main(String[] args) {
        CompletableFuture[] futures = new CompletableFuture[10];

        IntStream.range(0, 10).forEach(index -> {
            CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                long start = System.currentTimeMillis();
                System.out.println(Thread.currentThread().getName() + " start");
                try {
                    Thread.sleep(new SecureRandom().nextInt(10) * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long end = System.currentTimeMillis();
                System.out.println(Thread.currentThread().getName() + " done, duration: " + (end - start));
            });

            futures[index] = future;
        });

        CompletableFuture.allOf(futures).join();

        System.out.println(Thread.currentThread().getName() + " done");
    }
}
